local xiuhua = fk.CreateSkill{
  name = "wk_heg__xiuhua",
  tags = {Skill.Compulsory}
}
Fk:loadTranslationTable{
  ["wk_heg__xiuhua"] = "羞花",
  [":wk_heg__xiuhua"] = "锁定技，出牌阶段开始时，你令所有手牌数小于你的角色选择一项：1.弃置一张手牌；2.暗置一张武将牌且本回合不能明置之。",
  ["wk_heg__xiuhua-discard"] = "弃置一张手牌",
  ["wk_heg__xiuhua-hide"] = "暗置武将牌且本回合不能明置",
  ["@wk_heg__xiuhua_reveal-turn"] = "羞花 禁亮",

}

local HG = require "packages/new_heg_diy_my/util"
local H = require "packages/ol_hegemony/util"
local U = require "packages/utility/utility"

xiuhua:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(self) and player == target and player.phase == Player.Play then
      local tos = table.filter(player.room:getAlivePlayers(), function (p)
        return p:getHandcardNum() < player:getHandcardNum()
      end)
      if #tos > 0 then
        event:setCostData(self, {tos = tos})
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local tos = event:getCostData(self).tos  ---@type ServerPlayer[]
    local room = player.room
    for _, p in ipairs(tos) do
      if not p.dead then
        local choices = {}
        if table.find(p:getCardIds("h"), function (id) return not p:prohibitDiscard(id) end) then
          table.insert(choices, "wk_heg__xiuhua-discard")
        end
        if H.getGeneralsRevealedNum(p) == 2 then
          local disable_choices = {}
          if not (p.general ~= "anjiang" and not p.general:startsWith("blank_") and not string.find(p.general, "lord")) then -- 耦合君主
            table.insert(disable_choices, p.general)
          end
          if not (p.deputyGeneral ~= "anjiang" and not p.deputyGeneral:startsWith("blank_")) then
            table.insert(disable_choices, p.deputyGeneral)
          end
          if #disable_choices ~= 2 then
            table.insert(choices, "wk_heg__xiuhua-hide")
          end
        end
        if #choices > 0 then
          local choice = room:askToChoice(p,{
            choices = choices, skill_name = xiuhua.name})
          if choice == "wk_heg__xiuhua-discard" then
            room:askToDiscard(p, {
              min_num = 1,
              max_num = 1,
              include_equip = false,
              skill_name = xiuhua.name,
              cancelable = false})
          else
            local isDeputy = H.doHideGeneral(room, p, p , xiuhua.name)
            room:setPlayerMark(p, "@wk_heg__xiuhua_reveal-turn", H.getActualGeneral(p, isDeputy))
            local record = type(p:getMark(MarkEnum.RevealProhibited .. "-turn")) == "table" and p:getMark(MarkEnum.RevealProhibited .. "-turn") or {}
            table.insert(record, isDeputy and "d" or "m")
            room:setPlayerMark(p, MarkEnum.RevealProhibited .. "-turn", record)
          end
        end
      end
    end
  end
})

return xiuhua
